<template>
	<view class="container">
		<!-- 图片列表 -->
		<view v-if="scoreData.length > 0" class="image-container">
			<view v-for="(item, index) in scoreData" :key="item.id || index" class="image-item-wrapper">
				<image :src="item.goodsImage" mode="widthFix" class="image-item" @error="onImageError(index)"
					@click="goPreview(item.goodsId)"></image>
			</view>
		</view>

		<!-- 空状态提示 -->
		<view v-else class="empty-tip">
			暂无评分记录
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from "vue";
	import {
		apiGetScoreList
	} from "@/api/apis.js";

	import {
		fixMinioUrl
	} from "@/utils/img-url.js"

	const scoreData = ref([]);

	// 加载评分数据
	const loadScore = async () => {
		let user;
		// #ifdef MP
		user = uni.getStorageSync("userInfo");
		// #endif


		// #ifndef MP
		user = JSON.parse(uni.getStorageSync("userInfo") || "{}");
		// #endif


		if (!user || !user.id) {
			console.warn("用户未登录或用户信息缺失");
			return;
		}

		const form = {
			userId: user.id,
			pageNum: 1,
			pageSize: 10
		};

		try {
			const res = await apiGetScoreList(form);
			if (res.data?.list) {
				scoreData.value = res.data.list;
			} else {
				scoreData.value = [];
			}
		} catch (error) {
			console.error("获取评分列表失败：", error);
			scoreData.value = [];
		}
	};

	// 图片加载失败处理（可选）
	const onImageError = (index) => {
		console.log(`图片加载失败：索引 ${index}`);
	};

	onMounted(() => {
		loadScore();
	});


	const goPreview = (id) => {
		uni.navigateTo({
			url: `/pages/preview/preview?id=${id}`
		})
	}
</script>

<style scoped lang="scss">
	$primary-color: #007aff;
	$text-dark: #333;
	$border-radius: 12rpx;

	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 20rpx 10rpx;
		background-color: #f8f9fc;
		min-height: 100vh;
	}

	.image-container {
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		gap: 20rpx;
		width: 100%;
		padding: 0 10rpx;
		box-sizing: border-box;
	}

	.image-item-wrapper {
		// 每行两个：50% 宽度减去间距
		flex: 0 0 calc(50% - 20rpx);
		width: calc(50% - 20rpx);
		overflow: hidden;
		border-radius: $border-radius;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
		background-color: #fff;
	}

	.image-item {
		width: 100%;
		height: auto; // 高度由图片内容自动撑开
		display: block;
		border-radius: $border-radius;
	}

	.empty-tip {
		color: #999;
		font-size: 28rpx;
		margin-top: 60rpx;
		font-style: italic;
	}
</style>